package com.pingba.controller;

import com.pingba.common.Constant;
import com.pingba.entity.User;
import com.pingba.entity.location.City;
import com.pingba.entity.location.District;
import com.pingba.entity.location.Province;
import com.pingba.service.UserService;
import com.pingba.service.location.CityService;
import com.pingba.service.location.DistrictService;
import com.pingba.service.location.ProvinceService;
import com.pingba.util.ReturnHelp;
import com.pingba.util.helper.JsonHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2015/12/23 0023.
 */
@Controller
@RequestMapping(value = "/pingba/area", method = {RequestMethod.GET, RequestMethod.POST})
public class AreaController {

    @Autowired
    private CityService cityService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private UserService userService;

    @RequestMapping("test")
    @ResponseBody
    public String test() {
        List<Province> provinceList = provinceService.getAllProvinces();
        for (Province p : provinceList) {
            System.out.println(p);
            List<City> cityList = cityService.getCityOfProvince(p);
            for (City city : cityList) {
                System.out.println("\t" + city);
                List<District> districtList = districtService.getDistrictOfCity(city);
                for (District district : districtList) {
                    System.out.println("\t\t" + district);
                }
            }
        }
        return "1111";
    }


    @RequestMapping("province")
    @ResponseBody
    public String getProvices() {
        List<Province> provinceList = provinceService.getAllProvinces();
        return ReturnHelp.getReturnString(true, JsonHelp.jsonFromObject(provinceList));
    }

    @RequestMapping("cityOfProvince")
    @ResponseBody
    public String cityOfProvince(@RequestParam("provinceId") long provinceId) {
        Province province = new Province();
        province.setId(provinceId);
        List<City> cityList = cityService.getCityOfProvince(province);
        return ReturnHelp.getReturnString(true, JsonHelp.jsonFromObject(cityList));
    }


    @RequestMapping("districtOfCity")
    @ResponseBody
    public String districtOfCity(@RequestParam("cityId") long cityId) {
        City city = new City();
        city.setId(cityId);
        List<District> districtList = districtService.getDistrictOfCity(city);
        return ReturnHelp.getReturnString(true, JsonHelp.jsonFromObject(districtList));
    }

    public String saveProvince(HttpSession session, @RequestParam("province") String province) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        if (user == null) {
            return ReturnHelp.getReturnString(false, Constant.NeedLoginMsg);
        }
        session.setAttribute(Constant.ProvinceAttr, province);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }

    public String saveCity(HttpSession session, @RequestParam("city") String city) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        if (user == null) {
            return ReturnHelp.getReturnString(false, Constant.NeedLoginMsg);
        }
        session.setAttribute(Constant.CityAttr, city);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }

    public String saveDistrict(HttpSession session, @RequestParam("district") String district) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        if (user == null) {
            return ReturnHelp.getReturnString(false, Constant.NeedLoginMsg);
        }
        session.setAttribute(Constant.DistrictAttr, district);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }


    @RequestMapping("saveArea")
    @ResponseBody
    public String saveAreaInfo(HttpSession session, @RequestParam("province") String province, @RequestParam("city") String city, @RequestParam("district") String district) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        if (user == null) {
            return ReturnHelp.getReturnString(false, Constant.NeedLoginMsg);
        }
        user.setProvince(province);
        user.setCity(city);
        user.setDistrict(district);
        userService.updateLocationInfo(user);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }

}
